package com.rabbitmqproducer.controller;

import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class SendMsgController {
	@Resource
	RabbitTemplate rabbitTemplate;
	@GetMapping("/sendDirectMsg")
	String sendDirectMsg(){
		String messageId = UUID.randomUUID().toString();
		String messageData = "This is My Direct Message!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.convertAndSend("MyDirectExchange","MyDirectRouting",map);
		return "消息发送成功";
	}
	@GetMapping("/sendFanoutMsg")
	String sendFanoutMsg(){
		String messageId = UUID.randomUUID().toString();
		String messageData = "This is My Fanout Message!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.convertAndSend("fanoutExchange",null,map);
		return "消息发送成功";
	}
	@GetMapping("/sendTopicMsg1")
	String sendTopicMsg1(){
		String messageId = UUID.randomUUID().toString();
		String messageData = "My First Topic Message!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.convertAndSend("topicExchange","topic.First",map);
		return "消息发送成功";
	}
	@GetMapping("/sendTopicMsg2")
	String sendTopicMsg2(){
		String messageId = UUID.randomUUID().toString();
		String messageData = "My Second Topic Message!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.convertAndSend("topicExchange","topic.Second",map);
		return "消息发送成功";
	}
	@GetMapping("/msgAck1")
	String msgAck1(){
		String messageId = UUID.randomUUID().toString();
		String messageData = "this is exchange_null message!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.convertAndSend("exchange_null","MyDirectRouting",map);
		return "消息发送成功";
	}
	@GetMapping("/msgAck2")
	String msgAck2(){
		String messageId = UUID.randomUUID().toString();
		String messageData = "this is AloneDirectExchange message!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.convertAndSend("AloneDirectExchange","MyDirectRouting",map);
		return "消息发送成功";
	}
	@GetMapping("/sendConfirmMsg")
	String sendConfirmMsg(){
		String messageId = UUID.randomUUID().toString();
		String messageData = "This is My Confirm message!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.convertAndSend("ConfirmExchange","ConfirmRouting",map);
		return "消息发送成功";
	}
}
